package javaseof10day.day9.pm.demo1;




import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
//���ڶ����ݿ���в�������
public class SqlHelper {
 //����������ݿ���Ҫ�Ķ���
 PreparedStatement ps=null;
 Connection ct=null;
 ResultSet rs=null;
 String url="jdbc:mysql://localhost:3306/it123blog?useSSL=true&characterEncoding=utf-8";
 String user="root";
 String passwd="";
 String driver="com.mysql.jdbc.Driver";
 
 //�ر����ݿ���Դ
 public void close()
 {
  //�ر�
  try{
   if(rs!=null) rs.close();
   if(ps!=null) ps.close();
   if(ct!=null) ct.close();
  }catch(Exception e){
   e.printStackTrace();
  }
 }
 //д��һ������Ҫע��ķ���
 public ResultSet queryExcecute(String sql)
 {
  try{
   //1.��������
   Class.forName(driver);
   
   //2.�õ�����
   ct=DriverManager.getConnection(url,user,passwd);
   
   //3.����ps
   ps=ct.prepareStatement(sql);
   rs=ps.executeQuery();
 }catch(Exception e){
  e.printStackTrace();
 }finally{
  //�ر���Դ
 }
 return rs;
 }
 
 //��ѯ���ݿ�Ĳ���
 public ResultSet queryExecute(String sql,String []paras)
 {
   try{
    //1.��������
    Class.forName(driver);
    //2.�õ�����
    ct=DriverManager.getConnection(url,user,passwd);
    //3.����ps
    ps=ct.prepareStatement(sql);
    //��ps�ģ���ֵ
    for(int i=0;i<paras.length;i++)
    {
     ps.setString(i+1, paras[i]);
    }
    rs=ps.executeQuery();
  }catch(Exception e){
   e.printStackTrace();
  }finally{
   //�ر���Դ
  }
  return rs;
 }
 
 //����ɾ�ĺ���һ��
 public boolean updExecute(String sql,String []paras)
 {
  boolean b=true;
  try{
   //1.��������
   Class.forName(driver);
   //2.�õ�����
   ct=DriverManager.getConnection(url,user,passwd);
   //3.����ps
   ps=ct.prepareStatement(sql);
   //��ps�ģ���ֵ
   for(int i=0;i<paras.length;i++)
   {
    ps.setString(i+1, paras[i]);
   }
   //4.ִ�в���
   if(ps.executeUpdate()!=1)
   {
    b=false;
   }

  }catch(Exception e){
   b=false;
   e.printStackTrace();
  }finally{
   this.close();
  }
  return b;
 }
 }